package com.easychat.entity.enums;

import org.apache.commons.lang3.StringUtils;

public enum JoinTypeEnum {
    ALLOW(0, "直接加入"),
    PENDING_REVIEW(1, "需要审核");

    //类型
    private Integer type;
    //描述
    private String description;

    JoinTypeEnum(Integer type, String description) {
        this.type = type;
        this.description = description;
    }

    public Integer getType() {
        return type;
    }

    public String getDescription() {
        return description;
    }

    /**
     * 返回指定名称的枚举对象
     * @param name
     * @return
     */
    public static JoinTypeEnum getByName(String name) {
        try {
            if (StringUtils.isEmpty(name)) {
                //传入字符串不合法，返回null
                return null;
            }
            return JoinTypeEnum.valueOf(name.toUpperCase());//如果名称转为大写也没有相匹配的枚举对象，会产生异常
        } catch (IllegalArgumentException e) {
            //产生异常，返回null
            return null;
        }
    }

    public static JoinTypeEnum getByType(Integer type) {
        for (JoinTypeEnum joinTypeEnum : JoinTypeEnum.values()) {
            if (type.equals(joinTypeEnum.getType())) {
                return  joinTypeEnum;
            }
        }
        return null;
    }
}
